package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.OrderUserShenheParam;
import com.serkol.ams.bean.PayMentReslt;
import com.serkol.ams.entity.*;
import com.serkol.ams.mapper.OrderMasterMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.*;
import com.serkol.ams.service.agent.XahtanService;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultCode;
import com.serkol.exception.AuthException;
import com.serkol.exception.SucsessException;
import com.serkol.handler.MyLambdaQueryWrapper;
import com.serkol.utils.DateFormat;
import com.serkol.utils.DateUtils;
import com.serkol.utils.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author pingan
 * @description 针对表【order_master】的数据库操作Service实现
 * @createDate 2022-12-03 00:16:38
 */
@Service
public class OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster>
        implements OrderMasterService {

    @Value("${agentType}")
    private int agentType;

    @Autowired
    private OrderDetailService detailService;


    @Autowired
    private UsersService userService;

    @Autowired
    private AgentService agentService;
    @Autowired
    private AgentInvitationRelationshipService agentInvitationRelationshipService;

    @Autowired
    private AgentInvitationRelationshipLogService logService;
    @Autowired
    private AgentRechangeLogService agentRechangeLogService;

    @Autowired
    private UsersWalletService walletService;

    @Autowired
    private XahtanService xahtanService;

    @Autowired
    private UsersAddressService usersAddressService;
    @Autowired
    private ShopService shopService;

    @Override
    public Map<String, Object> myOrderStastica(Date startDate, Date endDate) {
        System.out.println(startDate);
        System.out.println(endDate);
        LoginLog loginLog = AuthConstant.getLoginLog();
        MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.dateBetween(OrderMaster::getCreateDate, DateFormat.DATE, startDate, endDate).eq(OrderMaster::getUserId, loginLog.getUserId());

        queryWrapper.select("COUNT(*) as  count,SUM(PAYMENT_AMOUNT) price,SUM(QUANTITY) quantity");
        Map<String, Object> map = this.getMap(queryWrapper);
        if (map == null) {
            map = new HashMap<String, Object>();

        }
        map.put("count", map.containsKey("count") ? map.get("count") : 0);
        map.put("price", map.containsKey("price") ? map.get("price") : 0.0);
        map.put("quantity", map.containsKey("quantity") ? map.get("quantity") : 0);

        return map;
    }

    @Override
    public Map<String, Object> teamOrderStastica(Date startDate, Date endDate) {

        LoginLog loginLog = AuthConstant.getLoginLog();
        List<Long> findChildrens = userService.getFindChildrens(loginLog.getUserId());
        findChildrens.add(0L);
        MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.dateBetween(OrderMaster::getCreateDate, DateFormat.DATE, startDate, endDate).in(OrderMaster::getUserId, findChildrens);
        queryWrapper.select("COUNT(*) as  count,SUM(PAYMENT_AMOUNT) price,SUM(QUANTITY) quantity");
        Map<String, Object> map = this.getMap(queryWrapper);
        if (map == null) {
            map = new HashMap<String, Object>();

        }
        map.put("count", map.containsKey("count") ? map.get("count") : 0);
        map.put("price", map.containsKey("price") ? map.get("price") : 0.0);
        map.put("quantity", map.containsKey("quantity") ? map.get("quantity") : 0);

        return map;
    }

    @Override
    public MyPage<Map<String, Object>> findByPageList(int pageSize, int current, int type, int paymentMethod, int paymentStatus, String userName, String sn, String partySn) {
        QueryWrapper<OrderMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getOrderFeild());
        if (type > -1) {
            queryWrapper.lambda().eq(OrderMaster::getCloudType, type);
        }
        if (paymentStatus > 0) {
            queryWrapper.lambda().eq(OrderMaster::getPaymentStatus, paymentStatus);
        }
        if (paymentMethod > 0) {
            queryWrapper.lambda().eq(OrderMaster::getPaymentMethod, paymentMethod);
        }
        if (StringUtils.isNotEmpty(userName)) {
            queryWrapper.lambda().inSql(OrderMaster::getUserId, SqlFeildModal.getAuthNameLike(userName));
        }
        if (StringUtils.isNotEmpty(sn)) {
            queryWrapper.lambda().eq(OrderMaster::getSn, sn);
        }
        if (StringUtils.isNotEmpty(partySn)) {
            queryWrapper.lambda().eq(OrderMaster::getPartySn, partySn);
        }
        queryWrapper.orderByDesc("ID");
        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(mapPage);
    }


    @Transactional(rollbackFor = {RuntimeException.class, AuthException.class, Exception.class})
    @Override
    public boolean saveAgentOrderDetails(OrderMaster order, List<OrderDetail> orderDetails, long agentId) throws AuthException, SucsessException {
        order.setQuantity(getCount(orderDetails));
        if (agentType == 2) {
            boolean b = saveAgentOrderDetailsPaymet2(order, orderDetails, agentId);
            System.out.println("saveAgentOrderDetails:" + b);
            if (b) {
                order.setIsPay(1);
                order.setPaymentStatus(2);
                order.setStatus(2);
                this.updateById(order);

                List<Long> parentUsersLevelIds = userService.getParentUsersLevelIds(order.getUserId());
                parentUsersLevelIds.add(order.getUserId());
                if (parentUsersLevelIds.size() > 0) {
                    //  parentUsersLevelIds.add(order.getUserId());
                    boolean byParenUsersPriceAll = findByParenUsersPriceAll(parentUsersLevelIds);
                    System.out.println("saveAgentOrderDetails2:" + byParenUsersPriceAll);
                    return byParenUsersPriceAll;
                } else {
                    return true;
                }

            } else {
                return false;
            }
        } else if (agentType == 3) {
            return saveAgentOrderDetailsPaymet1(order, orderDetails, agentId);
        } else if (agentType == 4) {
            boolean b = saveAgentOrderDetailsPaymet4(order, orderDetails, agentId);
            if (b) {
                order.setIsPay(1);
                order.setPaymentStatus(2);
                order.setStatus(2);
                this.updateById(order);
                List<Long> parentUsersLevelIds = userService.getParentUsersLevelIds(order.getUserId());
                parentUsersLevelIds.add(order.getUserId());
                if (parentUsersLevelIds.size() > 0) {
                    //  parentUsersLevelIds.add(order.getUserId());
                    boolean byParenUsersPriceAll = findByParenUsersPriceAll(parentUsersLevelIds);
                    System.out.println("saveAgentOrderDetails2:" + byParenUsersPriceAll);
                    return byParenUsersPriceAll;
                } else {
                    return true;
                }
            }
            return b;
        } else {
            return saveAgentOrderDetailsPaymet1(order, orderDetails, agentId);
        }

    }

    private boolean findByParenUsersPriceAll(List<Long> parentUsersLevelIds) {
        System.out.println("-1--------------------USER_ID:0000------------------");
        System.out.println(parentUsersLevelIds.size());
        AtomicBoolean flag = new AtomicBoolean(true);
        for (Long usersid : parentUsersLevelIds) {
            List<Long> findChildrens = userService.getFindChildrens(usersid);
            //   findChildrens.add(usersid);
            System.out.println("-1--------------------USER_ID:" + usersid + "------------------");
            System.out.println(findChildrens.size());
            boolean b = kaspiNatijaDispose(usersid, findChildrens);
           /* if(b){
                b=findByParenUsersPriceAll(findChildrens);
            }*/
            System.out.println("findByParenUsersPriceAll:" + b);
            flag.set(b);
        }

        return flag.get();
    }

    private boolean kaspiNatijaDispose(Long usersid, List<Long> findChildrens) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        AtomicBoolean flag = new AtomicBoolean(true);
        findChildrens.add(usersid);

        if (findChildrens.size() > 0) {
            MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
            queryWrapper.sum(OrderMaster::getPaymentAmount).as("paymentAmount");

            queryWrapper.in(OrderMaster::getUserId, findChildrens).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2).last("LIMIT 1");
            OrderMaster next = this.getOne(queryWrapper);
            if (next != null) {
                BigDecimal paymentAmount = next.getPaymentAmount();
                if (paymentAmount != null) {
                    Users byUserId = userService.findByUserId(usersid);
                    Long agentId = byUserId.getAgentId();
                    if (agentId == null) {
                        agentId = 1L;
                    }
                    Agent agent = agentService.getByAgent(paymentAmount, agentId);
                    if (agent != null) {
                        if (!Objects.equals(agentId, agent.getId()) && agent.getId() > agentId) {


                            LambdaUpdateWrapper<Users> usersMyLambdaQueryWrapper = new LambdaUpdateWrapper<>();

                            usersMyLambdaQueryWrapper.eq(Users::getId, usersid).set(Users::getAgentId, agent.getId());
                            if (agent.getId() > 1) {
                                usersMyLambdaQueryWrapper.set(Users::getAgentState, 1);
                            } else {
                                usersMyLambdaQueryWrapper.set(Users::getAgentState, 0);
                            }
                            boolean update = userService.update(usersMyLambdaQueryWrapper);
                            if (update) {
                                AgentRechangeLog agentRechangeLog = new AgentRechangeLog();
                                agentRechangeLog.setOperatorId(loginLog.getUserId());
                                agentRechangeLog.setAgentId(agent.getId());
                                agentRechangeLog.setRemark("自动升级");
                                agentRechangeLog.setUserId(usersid);

                                agentRechangeLog.setParentUserId(byUserId.getParentUserId());
                                agentRechangeLog.setOldAgentId(agentId);
                                agentRechangeLog.setOldParentUserId(byUserId.getParentUserId());
                                agentRechangeLogService.save(agentRechangeLog);
                            }
                            flag.set(update);
                        }
                    }
                }
            }
      /*      for (OrderMaster next : list) {
                if(next!=null) {
                    BigDecimal paymentAmount = next.getPaymentAmount();
                    if (paymentAmount != null) {
                        Long agentId = next.getAgentId();
                        LambdaUpdateWrapper<Users> usersMyLambdaQueryWrapper = new LambdaUpdateWrapper<>();
                        usersMyLambdaQueryWrapper.setSql("AGENT_ID=(SELECT ID FROM  agent WHERE PRICE<=" + paymentAmount + " AND ID>" + agentId + " OR ID=" + agentId + " ORDER BY PRICE DESC LIMIT 1)");
                        usersMyLambdaQueryWrapper.eq(Users::getId, next.getUserId());
                        boolean update = userService.update(usersMyLambdaQueryWrapper);
                        flag.set(update);
                    }
                }
            }*/
        }


        return flag.get();
    }


    @Transactional
    public boolean saveAgentOrderDetailsPaymet2(OrderMaster order, List<OrderDetail> orderDetails, long agentId) throws AuthException, SucsessException {

        List<Long> byGoodsIds = agentInvitationRelationshipService.findByGoodsIds(agentId);
        List<OrderDetail> goodsByList = new ArrayList<>();
        List<OrderDetail> goodsByList1 = new ArrayList<>();
        Iterator<OrderDetail> iterator = orderDetails.iterator();
        while (iterator.hasNext()) {
            OrderDetail next = iterator.next();
            next.setOrderId(order.getId());
            next.setAddressId(order.getAddressId());

            if (byGoodsIds.contains(next.getGoodsId())) {
                goodsByList.add(next);
            } else {
                goodsByList1.add(next);
            }
        }

        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" + order.getUserId());
        Users my = userService.findByUserId(order.getUserId());
        //  my.setAgentId(order.getAgentId());
        Users byUserId = null;
        Users byUserIdParent = null;
        Users byUserIdParentParent = null;
        if (my.getParentUserId() != null) {
            byUserId = userService.findByUserId(my.getParentUserId());
            if (byUserId.getParentUserId() != null) {
                byUserIdParent = userService.findByUserId(byUserId.getParentUserId());

                if (byUserIdParent.getParentUserId() != null) {
                    byUserIdParentParent = userService.findByUserId(byUserIdParent.getParentUserId());
                }
                System.out.println(byUserId);
            }
        }


        System.out.println(byUserId);
        System.out.println(order);
        if (byUserId != null) {
            AgentInvitationRelationship one = null;
            if (order.getIsUpgrade() == 1) {
                System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKLLLLLLLLL");
                System.out.println(byUserId);
                System.out.println(byUserIdParent);
                one = agentInvitationRelationshipService.getAgentUpgradeInfo(byUserId.getAgentId(), my.getAgentId(), agentId, order.getIsActivty(), -1);
                System.out.println(one);
                if (one != null) {
                    one.setPrice(one.getUpgradePrice());
                }

            } else {
                System.out.println("PARRRRRR:" + byUserId.getAgentId() + "-------------" + agentId);
                one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), agentId, order.getIsActivty(), -1);
            }
            if (one.getIsPrice() == 1) {
                one.setPrice(goPersent(order.getPaymentAmount(), one.getPrice()));
            }
            System.out.println(one);
            //  byUserId.setAgentId(agentId);
            if (one != null) {
                int type = 1;
                BigDecimal price = one.getPrice();
                if (order.getIsUpgrade() == 1) {
                    type = 2;
                    // price = price.subtract(one.getReduceUpgradePrice());
                }
                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-1");
                AgentInvitationRelationshipLog log = new AgentInvitationRelationshipLog();
                log.setAgentId(agentId);
                log.setRelationAgentId(byUserId.getAgentId());
                log.setOrderId(order.getId());
                log.setUserId(my.getId());
                log.setRelationUserId(my.getParentUserId());
                log.setPrice(price);
                log.setType(type);
                boolean save1 = logService.save(log);
                if (save1) {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-7");
                    AtomicBoolean flag = new AtomicBoolean(false);
                    boolean wsave = false;
                    if (price != null) {

                        wsave = assignmentPrice(my, order.getId(), byUserId, price);
                        flag.set(wsave);
                    }
                    if (wsave) {
                        if (byUserIdParent != null) {
//                    boolean a = getParentUserLevel1(byUserId, byUserIdParent.getAgentId(), byUserIdParent, order.getId(), goodsByList, goodsByList1, order.getIsActivty());
                            PayMentReslt a1 = getParentUserLevel(my, byUserIdParent.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());

                            //  PayMentReslt a1 = parentWalletPlus((byUserId.getAgentId() == 2 && my.getAgentId() == 2), my, order, byUserId, goodsByList, goodsByList1);
                            System.out.println(a1);
                            byUserId = a1.getUsers();
                            if (a1.isSuccess()) {
                                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB4");
                                my.setAgentState(1);
                                userService.updateById(my);
                                return true;
                            } else {
                                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB8B-5");
                                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                            }
                        }
                    }
                    if (flag.get()) {
                        my.setAgentState(1);
                        my.setAgentId(agentId);
                        userService.updateById(my);
                    }
                    return flag.get();
                } else {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-6");
                    throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                }

            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            }
        } else {
            return false;
        }

    }

    private PayMentReslt parentWalletPlus(boolean flag, Users my, OrderMaster order, Users paren, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1) throws AuthException, SucsessException {
        BigDecimal price = new BigDecimal(30);
        BigDecimal priceL = new BigDecimal(60);
        Long parentId = paren.getParentUserId();
        System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY-1");
        System.out.println(paren);
        if (parentId != null) {
            if (paren.getAgentId() == 3) {
                Users byUserId1 = userService.findByUserId(parentId);
                if (byUserId1 != null && byUserId1.getAgentId() == 4) {
                    BigDecimal a = (flag ? priceL : price);
                    BigDecimal multiply = a.multiply(new BigDecimal(order.getQuantity()));
                    boolean b = assignmentPrice(my, order.getId(), byUserId1, multiply);
                    if (b) {
                        if (byUserId1.getParentUserId() != null) {
                            System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY01");
                            // Users byUserId = userService.findByUserId(byUserId1.getParentUserId());
                            return getParentUserLevel(my, my.getAgentId(), byUserId1, order, goodsByList, goodsByList1, order.getIsActivty());

                        } else {
                            System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY02");
                            return PayMentReslt.valid(byUserId1, b);
                        }

                    } else {
                        System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY03");
                        return PayMentReslt.valid(byUserId1, b, 102);
                    }

                } else {
                    System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY04");
                    // Users byUserId = userService.findByUserId(byUserId1.getParentUserId());
                    return getParentUserLevel(my, my.getAgentId(), paren, order, goodsByList, goodsByList1, order.getIsActivty());


                }
            } else {
                if (paren.getAgentId() == 2) {
                    Users byUserId1 = userService.findByUserId(parentId);
                    if (byUserId1 != null && byUserId1.getAgentId() == 4) {
                        BigDecimal multiply = priceL.multiply(new BigDecimal(order.getQuantity()));
                        boolean b = assignmentPrice(my, order.getId(), byUserId1, multiply);
                        if (b) {
                            System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY05");
                            return getParentUserLevel(my, my.getAgentId(), byUserId1, order, goodsByList, goodsByList1, order.getIsActivty());

                        } else {
                            System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY06");
                            return PayMentReslt.valid(byUserId1, b, 104);
                        }
                    } else {
                        System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY07");
                        return getParentUserLevel(my, my.getAgentId(), paren, order, goodsByList, goodsByList1, order.getIsActivty());

                    }

                } else {
                    System.out.println(paren);
                    System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY08");
                    PayMentReslt parentUserLevel = getParentUserLevel(my, my.getAgentId(), paren, order, goodsByList, goodsByList1, order.getIsActivty());

                    System.out.println(parentUserLevel);
                    System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY08-01");
                    return parentUserLevel;

                }

            }
        } else {
            System.out.println("YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY09");
            return PayMentReslt.success(paren);
        }
        //    return false;
    }

    public boolean saveAgentOrderDetailsPaymet4(OrderMaster order, List<OrderDetail> orderDetails, long agentId) throws AuthException {


        List<Long> byGoodsIds = agentInvitationRelationshipService.findByGoodsIds(agentId);
        List<OrderDetail> goodsByList = new ArrayList<>();
        List<OrderDetail> goodsByList1 = new ArrayList<>();
        Iterator<OrderDetail> iterator = orderDetails.iterator();
        while (iterator.hasNext()) {
            OrderDetail next = iterator.next();
            next.setOrderId(order.getId());
            next.setAddressId(order.getAddressId());

            if (byGoodsIds.contains(next.getGoodsId())) {
                goodsByList.add(next);
            } else {
                goodsByList1.add(next);
            }
        }

        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" + order.getUserId());
        Users byUserId = userService.findByUserId(order.getUserId());
        System.out.println(byUserId);
        Users byUserIdParent = userService.findByUserId(byUserId.getParentUserId());

        System.out.println(byUserIdParent);
        System.out.println(order);
        AgentInvitationRelationship one = null;
        if (order.getIsUpgrade() == 1) {
            System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKLLLLLLLLL");
            one = agentInvitationRelationshipService.getAgentUpgradeInfo(byUserIdParent.getAgentId(), byUserId.getAgentId(), agentId, order.getIsActivty(), -1);
            if (one != null) {
                one.setPrice(one.getUpgradePrice());
            }

        } else {
            System.out.println("PARRRRRR:" + byUserIdParent.getAgentId() + "-------------" + agentId);
            one = agentInvitationRelationshipService.getAgentInfo(byUserIdParent.getAgentId(), agentId, order.getIsActivty(), -1);
        }
        System.out.println(one);
        byUserId.setAgentId(agentId);
        if (one != null) {
            int type = 1;
            BigDecimal price = one.getPrice();
            if (order.getIsUpgrade() == 1) {
                type = 2;
                // price = price.subtract(one.getReduceUpgradePrice());
            }
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB1");
            AgentInvitationRelationshipLog log = new AgentInvitationRelationshipLog();
            log.setAgentId(agentId);
            log.setRelationAgentId(byUserIdParent.getAgentId());
            log.setOrderId(order.getId());
            log.setUserId(byUserId.getId());
            log.setRelationUserId(byUserId.getParentUserId());
            log.setPrice(price);
            log.setType(type);
            boolean save1 = logService.save(log);
            if (save1) {
                boolean wsave = false;
                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB2");
                AgentInvitationRelationship agentInfo = null;

                agentInfo = agentInvitationRelationshipService.getAgentInfo(byUserIdParent.getAgentId(), byUserId.getAgentId(), order.getIsActivty(), -1);

                if (agentInfo != null) {
                    wsave = walletService.reChange("مۇكاپات",order.getId(), byUserId.getId(), byUserIdParent.getId(), price, type);
                } else {
                    wsave = true;
                }
                if (wsave) {
                    return wsave;

                } else {
                    throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                }
            } else {
                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            }

        } else {
            // throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            return true;
        }


    }

    public boolean saveAgentOrderDetailsPaymet1(OrderMaster order, List<OrderDetail> orderDetails, long agentId) throws AuthException, SucsessException {


        List<Long> byGoodsIds = agentInvitationRelationshipService.findByGoodsIds(agentId);
        List<OrderDetail> goodsByList = new ArrayList<>();
        List<OrderDetail> goodsByList1 = new ArrayList<>();
        Iterator<OrderDetail> iterator = orderDetails.iterator();
        while (iterator.hasNext()) {
            OrderDetail next = iterator.next();
            next.setOrderId(order.getId());
            next.setAddressId(order.getAddressId());

            if (byGoodsIds.contains(next.getGoodsId())) {
                goodsByList.add(next);
            } else {
                goodsByList1.add(next);
            }
        }

        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" + order.getUserId());
        Users byUserId = userService.findByUserId(order.getUserId());
        System.out.println(byUserId);
        Users byUserIdParent = userService.findByUserId(byUserId.getParentUserId());

        System.out.println(byUserIdParent);
        System.out.println(order);
        AgentInvitationRelationship one = null;
        if(byUserIdParent!=null) {
            if (order.getIsUpgrade() == 1) {
                System.out.println("KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKLLLLLLLLL");
                one = agentInvitationRelationshipService.getAgentUpgradeInfo(byUserIdParent.getAgentId(), byUserId.getAgentId(), agentId, order.getIsActivty(), -1);
                if (one != null) {
                    one.setPrice(one.getUpgradePrice());
                }

            } else {
                System.out.println("PARRRRRR:" + byUserIdParent.getAgentId() + "-------------" + agentId);
                one = agentInvitationRelationshipService.getAgentInfo(byUserIdParent.getAgentId(), agentId, order.getIsActivty(), -1);
            }
            System.out.println(one);
            byUserId.setAgentId(agentId);
            if (one != null) {
                int type = 1;
                if (one.getIsPrice() == 1) {
                    one.setPrice(goPersent(order.getPaymentAmount(), one.getPrice()));
                }
                BigDecimal price = one.getPrice();

                if (order.getIsUpgrade() == 1) {
                    type = 2;
                    // price = price.subtract(one.getReduceUpgradePrice());
                }
                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB1");
                AgentInvitationRelationshipLog log = new AgentInvitationRelationshipLog();
                log.setAgentId(agentId);
                log.setRelationAgentId(byUserIdParent.getAgentId());
                log.setOrderId(order.getId());
                log.setUserId(byUserId.getId());
                log.setRelationUserId(byUserId.getParentUserId());
                log.setPrice(price);
                log.setType(type);
                boolean save1 = logService.save(log);
                if (save1) {
                    boolean wsave = false;
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB2");
                    AgentInvitationRelationship agentInfo = null;

                    agentInfo = agentInvitationRelationshipService.getAgentInfo(byUserIdParent.getAgentId(), byUserId.getAgentId(), order.getIsActivty(), -1);

                    if (agentInfo != null) {
                        wsave = walletService.reChange("مۇكاپات",order.getId(), byUserId.getId(), byUserIdParent.getId(), price, type);
                    } else {
                        wsave = true;
                    }
                    if (wsave) {
                        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB3");

                        PayMentReslt a = getParentUserLevel1(byUserId, byUserIdParent.getAgentId(), byUserIdParent, order, goodsByList, goodsByList1, order.getIsActivty());
                        if (a.isSuccess()) {
                            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB4");
                            byUserId.setAgentState(1);
                            userService.updateById(byUserId);
                            return true;
                        } else {
                            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                        }

                    } else {
                        throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                    }
                } else {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
                    throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
                }

            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            }
        }else {
            return true;
        }

    }

    @Transactional(rollbackFor = {RuntimeException.class, AuthException.class, Exception.class})
    @Override
    public boolean saveAgentOrderCreateDetails(OrderMaster order, List<OrderDetail> orderDetails) throws AuthException, SucsessException {
        order.setQuantity(getCount(orderDetails));
        if (agentType == 2) {
            boolean b = saveAgentOrderDetailsPaymet2(order, orderDetails, order.getAgentId());
            if (b) {
                order.setIsPay(1);
                order.setPaymentStatus(2);
                order.setStatus(2);
                this.updateById(order);
                List<Long> parentUsersLevelIds = userService.getParentUsersLevelIds(order.getUserId());
                parentUsersLevelIds.add(order.getUserId());
                if (parentUsersLevelIds.size() > 0) {

                    return findByParenUsersPriceAll(parentUsersLevelIds);
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } else if (agentType == 3) {
            return saveAgentOrderCreateDetailsPayment1(order, orderDetails);
        } else if (agentType == 4) {
            boolean b = saveAgentOrderDetailsPaymet4(order, orderDetails, order.getAgentId());
            if (b) {
                order.setIsPay(1);
                order.setPaymentStatus(2);
                order.setStatus(2);
                this.updateById(order);
                List<Long> parentUsersLevelIds = userService.getParentUsersLevelIds(order.getUserId());
                parentUsersLevelIds.add(order.getUserId());
                if (parentUsersLevelIds.size() > 0) {
                    //  parentUsersLevelIds.add(order.getUserId());
                    boolean byParenUsersPriceAll = findByParenUsersPriceAll(parentUsersLevelIds);
                    System.out.println("saveAgentOrderDetails2:" + byParenUsersPriceAll);
                    return byParenUsersPriceAll;
                } else {
                    return true;
                }
            }
            return b;
        } else {
            return saveAgentOrderCreateDetailsPayment1(order, orderDetails);
        }

    }

    public boolean saveAgentOrderCreateDetailsPayment1(OrderMaster order, List<OrderDetail> orderDetails) throws AuthException, SucsessException {
        Users byUserId = userService.findByUserId(order.getUserId());

        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");

        List<Long> byGoodsIds = agentInvitationRelationshipService.findByGoodsIds(byUserId.getAgentId());
        List<OrderDetail> goodsByList = new ArrayList<>();
        List<OrderDetail> goodsByList1 = new ArrayList<>();
        Iterator<OrderDetail> iterator = orderDetails.iterator();
        while (iterator.hasNext()) {
            OrderDetail next = iterator.next();
            next.setOrderId(order.getId());
            next.setAddressId(order.getAddressId());

            if (byGoodsIds.contains(next.getGoodsId())) {
                goodsByList.add(next);
            } else {
                goodsByList1.add(next);
            }
        }
        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS1");
        PayMentReslt a = getParentUserLevel1(byUserId, byUserId.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());
        if (a.isSuccess()) {
            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS2");
            return true;
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
        }


    }

    @Override
    public Map<String, Object> getInfo() {
        LoginLog loginLog = AuthConstant.getLoginLog();
        MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(OrderMaster::getCloudType, 0).eq(OrderMaster::getUserId, loginLog.getUserId());
        long count = this.count(queryWrapper);
        MyLambdaQueryWrapper<OrderMaster> queryWrapper1 = new MyLambdaQueryWrapper<>();
        queryWrapper1.eq(OrderMaster::getCloudType, 1).eq(OrderMaster::getUserId, loginLog.getUserId());
        long count1 = this.count(queryWrapper1);
        Map<String, Object> map = new HashMap<>();
        map.put("order", count);
        map.put("orderCloud", count1);
        return map;
    }

    @Override
    public List<Map<String, Object>> findByMyList(int status) {
        LoginLog loginLog = AuthConstant.getLoginLog();
        MyLambdaQueryWrapper<OrderMaster> lambdaQueryWrapper = new MyLambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderMaster::getUserId, loginLog.getUserId());
        lambdaQueryWrapper.orderByDesc(OrderMaster::getId);
        if (status > 0) {
            lambdaQueryWrapper.eq(OrderMaster::getStatus, status);
        }
        List<OrderMaster> list = this.list(lambdaQueryWrapper);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (OrderMaster orderMaster : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("order", orderMaster);
            List<Map<String, Object>> byOrderDetailList = detailService.findByOrderDetailList(orderMaster.getId());
            map.put("details", byOrderDetailList);
            maps.add(map);

        }


        return maps;
    }

    @Override
    public OrderMaster getBySn(String outTradeNo) {
        MyLambdaQueryWrapper<OrderMaster> lambdaQueryWrapper = new MyLambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderMaster::getSn, outTradeNo);
        lambdaQueryWrapper.last("limit 1");
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public OrderMaster findById(long orderId) {
        return this.getById(orderId);
    }

    @Override
    public Map<String, Object> findMapById(long orderId) {
        MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getOrderFeild());
        queryWrapper.eq(OrderMaster::getId, orderId);
        queryWrapper.last("LIMIT 1");
        Map<String, Object> map = this.getMap(queryWrapper);
        return map;
    }

    @Override
    public Map<String, Object> getUsersStastic() {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<OrderMaster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        long shipments = this.count(queryWrapper);
        map.put("shipments", shipments);

        long count = this.count();
        map.put("count", count);

        return map;
    }

    @Override
    public boolean savecloudDeliveryOrderDetails(OrderMaster order) {
        Users byUserId = userService.findByUserId(order.getUserId());
        Users byUserIdParent = userService.findByUserId(byUserId.getParentUserId());

        AgentInvitationRelationshipLog log = new AgentInvitationRelationshipLog();
        int type = 1;
        BigDecimal price = order.getPaymentAmount();
        if (order.getIsUpgrade() == 1) {
            type = 2;
            order.setAgentId(order.getAgentId());
            // price = price.subtract(one.getReduceUpgradePrice());
        }
        log.setAgentId(byUserId.getAgentId());
        log.setRelationAgentId(byUserIdParent.getAgentId());
        log.setOrderId(order.getId());
        log.setUserId(byUserId.getId());
        log.setRelationUserId(byUserId.getParentUserId());
        log.setPrice(price);
        log.setType(type);
        boolean save1 = logService.save(log);
        if (save1) {
            boolean wsave = false;
            wsave = walletService.reChange("يۈندەيفادىن  مۇكاپات ئايرىلدى",order.getId(), byUserId.getId(), byUserIdParent.getId(), price, type);
            if (wsave) {
                byUserId.setAgentState(1);
                return userService.updateById(byUserId);
            } else {
                return false;
            }
        } else {
            return false;
        }

    }


    private PayMentReslt getParentUserLevel(Users my, long myAgentId, Users users, OrderMaster order, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException, SucsessException {
        System.out.println("yyyyyyyyyyyyyyyyyyyyyyyyyy");
        System.out.println(users);
        Users byUserId = userService.findByUserId(users.getParentUserId());
        if (byUserId != null) {
            System.out.println(users);
            System.out.println(byUserId);
            System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
            System.out.println(byUserId.getAgentId() < users.getAgentId());
            if (byUserId.getAgentId() < users.getAgentId()) {
                System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
                Users byUserId1 = userService.findByUserId(byUserId.getParentUserId());
                System.out.println("----------------------------");
                //   System.out.println(byUserId1);
                if (byUserId1.getAgentId().equals(users.getAgentId())) {
                    PayMentReslt createAssignmentSuan = getCreateAssignmentSuan2(my, users.getAgentId(), order, byUserId1, goodsByList, goodsByList1, isActivty);


                    return createAssignmentSuan;
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId1.getAgentId(), users.getAgentId(), isActivty, -1);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, one.getParentPrice());
                        boolean b = assignmentPrice(my, order.getId(), byUserId1, bigDecimal);

                        if (b) {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                            if (byUserId.getParentUserId() != null) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                return getParentUserLevel(my, myAgentId, byUserId1, order, goodsByList, goodsByList1, isActivty);

                            } else {
                                return PayMentReslt.success(byUserId1);
                            }
                        } else {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                            return PayMentReslt.fail(byUserId1);
                        }


                    } else {
                        return PayMentReslt.success(byUserId1);
                    }

                }


            } else {

                if (byUserId.getAgentId().equals(users.getAgentId())) {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5");

                    System.out.println("--------------byUserId------------------------");
                    System.out.println(byUserId);
                    System.out.println("--------------users------------------------");
                    System.out.println(users);
                    PayMentReslt createAssignmentSuan2 = getCreateAssignmentSuan2(my, users.getAgentId(), order, byUserId, goodsByList, goodsByList1, isActivty);
                    System.out.println(createAssignmentSuan2);
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB15");
                    return createAssignmentSuan2;


                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), users.getAgentId(), isActivty, -1);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal parentPrice = one.getParentPrice();

                        if (parentPrice != null) {
                            BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, one.getParentPrice());
                            boolean b = assignmentPrice(my, order.getId(), byUserId, bigDecimal);
                            if (b) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                                if (byUserId.getParentUserId() != null) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                    return getParentUserLevel(my, myAgentId, byUserId, order, goodsByList, goodsByList1, isActivty);

                                } else {
                                    return PayMentReslt.success(byUserId);
                                }
                            } else {
                                return PayMentReslt.success(byUserId);
                            }
                        } else {
                            return PayMentReslt.success(byUserId);
                        }

                    } else {
                        return PayMentReslt.success(byUserId);
                    }
                }

            }
        } else {
            return PayMentReslt.success(users);
        }

    }

    private PayMentReslt getParentUserLevel1(Users my, long myAgentId, Users users, OrderMaster order, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException, SucsessException {
        System.out.println("yyyyyyyyyyyyyyyyyyyyyyyyyy");
        System.out.println(users);
        Users byUserId = userService.findByUserId(users.getParentUserId());
        if (byUserId != null) {

            System.out.println(users);
            System.out.println(byUserId);
            System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
            System.out.println(byUserId.getAgentId() < users.getAgentId());
            if (byUserId.getAgentId() < users.getAgentId()) {
                System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
                Users byUserId1 = userService.findByUserId(byUserId.getParentUserId());
                System.out.println("----------------------------");
                //   System.out.println(byUserId1);
                if (byUserId1.getAgentId().equals(users.getAgentId())) {
                    PayMentReslt createAssignmentSuan = getCreateAssignmentSuan(my, users.getAgentId(), order, byUserId1, goodsByList, goodsByList1, isActivty);


                    return createAssignmentSuan;
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId1.getAgentId(), users.getAgentId(), isActivty, -1);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, one.getParentPrice());
                        boolean b = assignmentPrice(my, order.getId(), byUserId1, bigDecimal);

                        if (b) {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                            if (byUserId.getParentUserId() != null) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                return getParentUserLevel1(my, myAgentId, byUserId1, order, goodsByList, goodsByList1, isActivty);

                            } else {
                                return PayMentReslt.success(byUserId1);
                            }
                        } else {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                            return PayMentReslt.fail(byUserId1);
                        }


                    } else {
                        return PayMentReslt.success(byUserId1);
                    }

                }


            } else {

                if (byUserId.getAgentId().equals(users.getAgentId())) {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5");
        /*            Users byParentAgentUserIdLP = userService.findByParentAgentUserIdLP(byUserId.getId(), byUserId.getAgentId());

                    System.out.println("--------------byUserId------------------------");
                    System.out.println(byUserId);
                    System.out.println("--------------users------------------------");
                    System.out.println(users);*/
                    PayMentReslt createAssignmentSuan2 = getCreateAssignmentSuan(my, users.getAgentId(), order, byUserId, goodsByList, goodsByList1, isActivty);
                    System.out.println(createAssignmentSuan2);
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB15");
                    return createAssignmentSuan2;
                    //  return getParentUserLevel1(my, myAgentId, byUserId, order, goodsByList, goodsByList1, isActivty);


                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), users.getAgentId(), isActivty, -1);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal parentPrice = one.getParentPrice();

                        if (parentPrice != null) {
                            BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, one.getParentPrice());
                            boolean b = assignmentPrice(my, order.getId(), byUserId, bigDecimal);
                            if (b) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                                if (byUserId.getParentUserId() != null) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                    return getParentUserLevel1(my, myAgentId, byUserId, order, goodsByList, goodsByList1, isActivty);

                                } else {
                                    return PayMentReslt.success(byUserId);
                                }
                            } else {
                                return PayMentReslt.success(byUserId);
                            }
                        } else {
                            return PayMentReslt.success(byUserId);
                        }

                    } else {
                        return PayMentReslt.success(byUserId);
                    }
                }

            }
        } else {
            return PayMentReslt.success(users);
        }

    }

/*    private boolean getParentUserLevel1(Users my, long myAgentId, Users users, OrderMaster order, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException {
        Users byUserId = userService.findByUserId(users.getParentUserId());
        if (byUserId != null) {
            System.out.println(users);
            System.out.println(byUserId);
            System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
            System.out.println(byUserId.getAgentId() < users.getAgentId());
            if (byUserId.getAgentId() < users.getAgentId()) {
                System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
                Users byUserId1 = userService.findByUserId(byUserId.getParentUserId());
                System.out.println("----------------------------");
                //   System.out.println(byUserId1);
                if (byUserId1.getAgentId().equals(users.getAgentId())) {
                    boolean createAssignmentSuan = getCreateAssignmentSuan(my, users.getAgentId(), order, byUserId1, goodsByList, goodsByList1, isActivty);
                    return createAssignmentSuan;
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId1.getAgentId(), users.getAgentId(), isActivty, -1);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, one.getParentPrice());
                        boolean b = assignmentPrice(my, order.getId(), byUserId1, bigDecimal);

                        if (b) {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                            if (byUserId.getParentUserId() != null) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                boolean parentUserLevel = getParentUserLevel1(my, myAgentId, byUserId1, order, goodsByList, goodsByList1, isActivty);
                                if (parentUserLevel) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                                }
                            }
                        }
                    }
                    return true;
                }


            } else {

                if (byUserId.getAgentId().equals(users.getAgentId())) {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5");

                    System.out.println("--------------byUserId------------------------");
                    System.out.println(byUserId);
                    System.out.println("--------------users------------------------");
                    System.out.println(users);
                    return getCreateAssignmentSuan(my, users.getAgentId(), order, byUserId, goodsByList, goodsByList1, isActivty);
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), users.getAgentId(), isActivty, -1);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal parentPrice = one.getParentPrice();

                        if (parentPrice != null) {
                            BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, one.getParentPrice());
                            boolean b = assignmentPrice(my, order.getId(), byUserId, bigDecimal);
                            if (b) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                                if (byUserId.getParentUserId() != null) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                    boolean parentUserLevel = getParentUserLevel1(my, myAgentId, byUserId, order, goodsByList, goodsByList1, isActivty);
                                    if (parentUserLevel) {
                                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                                        return true;
                                    } else {
                                        return true;
                                    }
                                } else {
                                    return true;
                                }
                            } else {
                                return true;
                            }
                        } else {
                            return true;
                        }

                    } else {
                        return true;
                    }
                }

            }
        } else {
            return true;
        }


    }*/

    @Transactional
    public boolean assignmentPrice(Users my, long orderId, Users byUserId, BigDecimal price) throws AuthException {
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-8");
        System.out.println(byUserId);
        System.out.println("price:" + price);
        if (price != null && price.intValue() > 0) {
            LoginLog loginLog = AuthConstant.getLoginLog();
            AgentInvitationRelationshipLog log = new AgentInvitationRelationshipLog();
            log.setAgentId(my.getAgentId());
            log.setRelationAgentId(byUserId.getAgentId());
            log.setOrderId(orderId);
            log.setUserId(my.getId());
            log.setRelationUserId(byUserId.getId());
            log.setPrice(price);
            log.setType(0);
            System.out.println(log);
            System.out.println("PRICE:" + price);
            boolean save = logService.save(log);
            if (save) {
                boolean wsave = walletService.reChange("مۇكاپات",orderId, my.getId(), byUserId.getId(), price, 2);
                if (wsave) {
                    return true;
                } else {
                    throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);

                }
            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);

            }
        } else {
            //  throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            return true;
        }
    }

    private BigDecimal assignmentPriceSuan(List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, BigDecimal price) {
        BigDecimal suanTotal = new BigDecimal(0);
        if (price == null) {
            price = new BigDecimal(0);
        }

        for (OrderDetail next : goodsByList) {
            BigDecimal parentPrice = new BigDecimal(0);
            parentPrice = parentPrice.add(price);
            parentPrice = parentPrice.multiply(BigDecimal.valueOf(next.getQuantity()));
            suanTotal = suanTotal.add(parentPrice);
        }
        for (OrderDetail next : goodsByList1) {
            BigDecimal parentPrice = new BigDecimal(0);
            parentPrice = parentPrice.add(price);
            parentPrice = parentPrice.multiply(BigDecimal.valueOf(next.getQuantity()));
            suanTotal = suanTotal.add(parentPrice);
        }
        return suanTotal;
    }

    private int getCount(List<OrderDetail> goodsByList) {
        BigInteger suanTotal = new BigInteger(String.valueOf(0));

        for (OrderDetail next : goodsByList) {

            suanTotal = suanTotal.add(BigInteger.valueOf(next.getQuantity()));
        }

        return suanTotal.intValue();
    }


    private boolean getCreateAssignment(Users my, Users byUserId, OrderDetail detail, Integer isActivty) throws AuthException {
        LoginLog loginLog = AuthConstant.getLoginLog();

        AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(loginLog.getAgentId(), byUserId.getAgentId(), isActivty, -1);
        if (one != null) {
            Map<Integer, Users> userList = userService.getParentUsersLevel(byUserId);
            //通过 lambda 表达式使用 forEach()
            AtomicBoolean flag = new AtomicBoolean(false);
            userList.forEach((key, value) -> {
                switch (key) {
                    case 1:
                        BigDecimal level1 = one.getLevel1();
                        level1 = level1.multiply(BigDecimal.valueOf(detail.getQuantity()));


                        boolean newValue = false;
                        try {
                            newValue = assignmentPrice(my, detail.getOrderId(), value, level1);
                        } catch (AuthException e) {
                            e.printStackTrace();
                        }
                        flag.set(newValue);

                        break;
                    case 2:
                        BigDecimal level2 = one.getLevel1();
                        level2 = level2.multiply(BigDecimal.valueOf(detail.getQuantity()));
                        try {
                            flag.set(assignmentPrice(my, detail.getOrderId(), value, level2));
                        } catch (AuthException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 3:
                        BigDecimal level3 = one.getLevel1();
                        level3 = level3.multiply(BigDecimal.valueOf(detail.getQuantity()));
                        try {
                            flag.set(assignmentPrice(my, detail.getOrderId(), value, level3));
                        } catch (AuthException e) {
                            e.printStackTrace();

                        }
                        break;
                }
            });
            return flag.get();
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
        }

    }

    // @Transactional
  /*  boolean               getCreateAssignmentSuan(Users my, long myAgentId, OrderMaster order, Users byUserId, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException {
        //   LoginLog loginLog = AuthConstant.getLoginLog();
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
        AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), myAgentId, isActivty, -1);
        if (one != null) {
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB7");
            System.out.println(one);
            Map<Integer, Users> userList = userService.getParentUsersLevel(byUserId);
            System.out.println("userList:" + userList.size());
            //通过 lambda 表达式使用 forEach()
            AtomicInteger index = new AtomicInteger(3);
            AtomicBoolean flag = new AtomicBoolean(false);
            userList.forEach((key, value) -> {
                System.out.println("KEY:" + key);
                System.out.println("NICK_NAME" + value.getNickname());
                BigDecimal level = one.getLevel1();
                BigDecimal level2 = one.getLevel2();
                BigDecimal level3 = one.getLevel3();
                switch (key) {
                    case 1:

                        if (level != null) {

                            if (one.getIsLevel() == 1) {
                                level = goPersent(level, order.getPaymentAmount());
                            }
                            BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, level);
                            boolean b = false;
                            try {
                                b = assignmentPrice(my, order.getId(), value, bigDecimal);
                                if (b) {
                                    index.set(1);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            flag.set(b);
                            if (level2 == null && level3 == null) {
                                if (userList.containsKey(2)) {


                                    Users users = userList.get(2);
                                    if (!users.getAgentId().equals(value.getAgentId())) {
                                        b = retPaymet(my, value, order, goodsByList, goodsByList1, isActivty);
                                    }
                                }
                            }
                        } else {
                            flag.set(true);
                        }

                        break;
                    case 2:

                        if (level2 != null) {
                            if (one.getIsLevel() == 1) {
                                level2 = goPersent(level2, order.getPaymentAmount());
                            }
                            BigDecimal bigDecimal2 = assignmentPriceSuan(goodsByList, goodsByList1, level2);
                            boolean b2 = false;
                            try {
                                b2 = assignmentPrice(my, order.getId(), value, bigDecimal2);
                                if (b2) {
                                    index.set(2);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            if (level3 == null) {
                                if (userList.containsKey(3)) {


                                    Users users = userList.get(2);
                                    if (!users.getAgentId().equals(value.getAgentId())) {
                                        b2 = retPaymet(my, value, order, goodsByList, goodsByList1, isActivty);
                                    }
                                }

                            }
                            flag.set(b2);
                        } else {
                            flag.set(true);
                        }
                        break;
                    case 3:

                        if (level3 != null) {
                            if (one.getIsLevel() == 1) {
                                level3 = goPersent(level3, order.getPaymentAmount());
                            }
                            BigDecimal bigDecimal3 = assignmentPriceSuan(goodsByList, goodsByList1, level3);
                            boolean b3 = false;
                            try {
                                b3 = assignmentPrice(my, order.getId(), value, bigDecimal3);
                                if (b3) {
                                    index.set(3);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            flag.set(b3);
                        } else {
                            flag.set(true);
                        }
                        break;
                }

            });
         *//*   if (index.get() < 3) {
                if (userList.containsKey(index.get())) {
                    Users users = userList.get(index.get());
                    if (users != null) {
                        Users byUserId1 = userService.findByUserId(users.getParentUserId());
                        boolean parentUserLevel1 = false;
                        try {
                            parentUserLevel1 = getParentUserLevel1(my, byUserId1.getAgentId(), byUserId1, orderId, goodsByList, goodsByList1);
                        } catch (AuthException e) {
                            e.printStackTrace();
                        }
                        flag.set(parentUserLevel1);
                    }
                }
            }*//*
            return flag.get();
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
        }

    }
*/
    PayMentReslt getCreateAssignmentSuan(Users my, long myAgentId, OrderMaster order, Users byUserId, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException, SucsessException {
        //   LoginLog loginLog = AuthConstant.getLoginLog();
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
        AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), myAgentId, isActivty, -1);
        if (one != null) {
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB7");
            System.out.println(one);
            Map<Integer, Users> userList = userService.getParentUsersLevel(byUserId);
            System.out.println("userList:" + userList.size());
            //通过 lambda 表达式使用 forEach()
            AtomicInteger index = new AtomicInteger(3);
            AtomicBoolean flag = new AtomicBoolean(false);
            BigDecimal level = one.getLevel1();
            BigDecimal level2 = one.getLevel2();
            BigDecimal level3 = one.getLevel3();
            if (userList.containsKey(1)) {
                Users users1 = userList.get(1);
                if (one.getIsLevel() == 1) {
                    level = goPersent(level, order.getPaymentAmount());
                }

                if (!byUserId.getAgentId().equals(users1.getAgentId())) {
                    level = one.getParentPrice();
                    if (one.getIsParent() == 1) {
                        //  level = goPersent(level, order.getPaymentAmount());
                    }
                }


                BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, level);
                boolean b = assignmentPrice(my, order.getId(), users1, bigDecimal);
                if (b) {
                    if (level2 != null) {
                        if (one.getIsLevel() == 1) {
                            level2 = goPersent(level2, order.getPaymentAmount());
                        }

                        if (userList.containsKey(2)) {
                            Users users2 = userList.get(2);
                            //     BigDecimal bigDecimal1 = goPriceValid(byUserId.getAgentId(), users1.getAgentId(), level2);
                            PayMentReslt br = retTreeSuan(my, myAgentId, level2, users2, users1, order, one, goodsByList, goodsByList1, isActivty);
                            System.out.println("OOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK01");
                            System.out.println(br);
                            System.out.println("LL:" + level3);
                            users2 = br.getUsers();
                            if (br.isSuccess()) {
                                if (level3 != null) {
                                    if (one.getIsLevel() == 1) {
                                        level3 = goPersent(level3, order.getPaymentAmount());
                                    }

                                    if (userList.containsKey(3)) {
                                        Users users3 = userList.get(3);
                                        PayMentReslt br1 = retTreeSuan(my, myAgentId, level3, users3, users2, order, one, goodsByList, goodsByList1, isActivty);
                                        System.out.println("OOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK02");
                                        System.out.println(br1);
                                        if (br1.isSuccess()) {
                                            if (users2.getAgentId() == 4 && users3.getAgentId() == 4) {
                                                return br;    //getParentUserLevel(my, my.getAgentId(), users2, order, goodsByList, goodsByList1, order.getIsActivty());
                                            } else if (users2.getAgentId() == 4) {
                                                return br;
                                            } else {
                                                if (users2.getAgentId().equals(users3.getAgentId())) {
                                                    Users byParentAgentUserIdLP = userService.findByParentAgentUserIdLP(users3.getId(), users3.getAgentId());

                                                    return getParentUserLevel1(my, my.getAgentId(), byParentAgentUserIdLP, order, goodsByList, goodsByList1, order.getIsActivty());
                                                    //  return parentWalletPlus((byParentAgentUserIdLP.getAgentId() == 2 && users1.getAgentId() == 2), my, order, byParentAgentUserIdLP, goodsByList, goodsByList1);
                                                } else {
                                                    return getParentUserLevel1(my, my.getAgentId(), users3, order, goodsByList, goodsByList1, order.getIsActivty());
                                                }
                                            }


                                        } else {
                                            return PayMentReslt.fail(users3);
                                        }
                                    } else {
                                        return PayMentReslt.success(users2);
                                    }
                                } else {
                                    PayMentReslt a1 = parentWalletPlus((users2.getAgentId() == 2 && users1.getAgentId() == 2), my, order, users2, goodsByList, goodsByList1);
                                    System.out.println("IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII01");
                                    System.out.println(a1);
                                    return a1;
                                }

                            } else {
                                return PayMentReslt.fail(users2);
                            }
                        } else {
                            return PayMentReslt.success(users1);
                        }

                    } else {
                        //      PayMentReslt a = parentWalletPlus((users1.getAgentId() == 2 && byUserId.getAgentId() == 2), my, order, users1, goodsByList, goodsByList1);
                        //      users1 = a.getUsers();
                        Users usersR = users1;
                        if (userList.containsKey(2)) {
                            Users users2 = userList.get(2);

                            if (users2.getAgentId().equals(users1.getAgentId())) {
                                Users byParentAgentUserIdLP = userService.findByParentAgentUserIdLP(users1.getId(), users1.getAgentId());

                                return getParentUserLevel1(my, my.getAgentId(), byParentAgentUserIdLP, order, goodsByList, goodsByList1, order.getIsActivty());
                                //  return parentWalletPlus((byParentAgentUserIdLP.getAgentId() == 2 && users1.getAgentId() == 2), my, order, byParentAgentUserIdLP, goodsByList, goodsByList1);
                            } else {
                                return getParentUserLevel1(my, my.getAgentId(), users1, order, goodsByList, goodsByList1, order.getIsActivty());
                            }


                        } else {

                            return getParentUserLevel1(my, my.getAgentId(), users1, order, goodsByList, goodsByList1, order.getIsActivty());
                        }
                        //       return a;
                    }

                } else {
                    return PayMentReslt.success(byUserId);

                }
            } else {
                return PayMentReslt.fail(byUserId);

            }


        } else {
            return PayMentReslt.fail(byUserId);

        }
   /*         userList.forEach((key, value) -> {
                System.out.println("KEY:" + key);
                System.out.println("NICK_NAME" + value.getNickname());

                switch (key) {
                    case 1:

                        if (level != null) {
                            if (!value.getAgentId().equals(byUserId.getAgentId())) {

                                level = one.getParentPrice();

                            }


                            BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, level);
                            boolean b = false;
                            try {
                                b = assignmentPrice(my, order.getId(), value, bigDecimal);
                                if (b) {
                                    index.set(1);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }

                            flag.set(b);
                            if (level2 == null) {
                                if (userList.containsKey(2)) {
                                    //    Users users = userList.get(1);
                                    if (!byUserId.getAgentId().equals(value.getAgentId())) {
                                        b = retPaymet1(my, value, order, goodsByList, goodsByList1, isActivty);
                                    }
                                }
                            }
                        } else {
                            flag.set(true);
                        }

                        break;
                    case 2:

                        if (level2 != null) {
                            Users users1 = userList.get(1);
                            if (!value.getAgentId().equals(users1.getAgentId())) {
                                level2 = one.getParentPrice();
                            }
                            BigDecimal bigDecimal2 = assignmentPriceSuan(goodsByList, goodsByList1, level2);
                            boolean b2 = false;
                            try {
                                b2 = assignmentPrice(my, order.getId(), value, bigDecimal2);
                                if (b2) {
                                    index.set(2);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            if (level3 == null) {
                                if (userList.containsKey(3)) {


                                    //  Users users = userList.get(2);
                                    if (!value.getAgentId().equals(users1.getAgentId())) {
                                        b2 = retPaymet1(my, value, order, goodsByList, goodsByList1, isActivty);
                                    }
                                }

                            }
                            flag.set(b2);
                        } else {
                            flag.set(true);
                        }
                        break;
                    case 3:

                        if (level3 != null) {
                            BigDecimal bigDecimal3 = assignmentPriceSuan(goodsByList, goodsByList1, level3);
                            boolean b3 = false;
                            try {
                                b3 = assignmentPrice(my, order.getId(), value, bigDecimal3);
                                if (b3) {
                                    index.set(3);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            flag.set(b3);
                        } else {
                            flag.set(true);
                        }
                        break;
                }

            });*/
         /*   if (index.get() < 3) {
                if (userList.containsKey(index.get())) {
                    Users users = userList.get(index.get());
                    if (users != null) {
                        Users byUserId1 = userService.findByUserId(users.getParentUserId());
                        boolean parentUserLevel1 = false;
                        try {
                            parentUserLevel1 = getParentUserLevel1(my, byUserId1.getAgentId(), byUserId1, orderId, goodsByList, goodsByList1);
                        } catch (AuthException e) {
                            e.printStackTrace();
                        }
                        flag.set(parentUserLevel1);
                    }
                }
            }*/

    }

    PayMentReslt getCreateAssignmentSuan2(Users my, long myAgentId, OrderMaster order, Users byUserId, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException, SucsessException {
        //   LoginLog loginLog = AuthConstant.getLoginLog();
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
        AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfo(byUserId.getAgentId(), myAgentId, isActivty, -1);
        if (one != null) {
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB7");
            System.out.println(one);
            Map<Integer, Users> userList = userService.getParentUsersLevel(byUserId);
            System.out.println("userList:" + userList.size());
            //通过 lambda 表达式使用 forEach()
            AtomicInteger index = new AtomicInteger(3);
            AtomicBoolean flag = new AtomicBoolean(false);
            BigDecimal level = one.getLevel1();
            BigDecimal level2 = one.getLevel2();
            BigDecimal level3 = one.getLevel3();
            if (userList.containsKey(1)) {
                Users users1 = userList.get(1);

                if (!byUserId.getAgentId().equals(users1.getAgentId())) {
                    level = one.getParentPrice();
                }

                BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, level);
                boolean b = assignmentPrice(my, order.getId(), users1, bigDecimal);
                if (b) {
                    if (level2 != null) {

                        if (userList.containsKey(2)) {
                            Users users2 = userList.get(2);
                            //     BigDecimal bigDecimal1 = goPriceValid(byUserId.getAgentId(), users1.getAgentId(), level2);
                            PayMentReslt br = retTreeSuan(my, myAgentId, level2, users2, users1, order, one, goodsByList, goodsByList1, isActivty);
                            System.out.println("OOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK01");
                            System.out.println(br);
                            System.out.println("LL:" + level3);
                            users2 = br.getUsers();
                            if (br.isSuccess()) {
                               /* if (level3 != null) {
                                    if (userList.containsKey(3)) {
                                        Users users3 = userList.get(3);
                                        PayMentReslt br1 = retTreeSuan(my, level3, users3, users2, order, one, goodsByList, goodsByList1, isActivty);
                                        System.out.println("OOOOOOOOOOOOOOOOOOOOOOKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK02");
                                        System.out.println(br1);
                                        if (br1.isSuccess()) {
                                            return br1;

                                        } else {
                                            return PayMentReslt.fail(users3);
                                        }
                                    } else {
                                        return PayMentReslt.success(users2);
                                    }
                                } else {
                                    PayMentReslt a1 = parentWalletPlus((users2.getAgentId() == 2 && users1.getAgentId() == 2), my, order, users2, goodsByList, goodsByList1);
                                    System.out.println("IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII01");
                                    System.out.println(a1);
                                    return a1;
                                }*/
                                if (users2.getAgentId() == 4 && users1.getAgentId() == 4) {
                                    return br;    //getParentUserLevel(my, my.getAgentId(), users2, order, goodsByList, goodsByList1, order.getIsActivty());
                                } else if (users2.getAgentId() == 4) {
                                    return br;
                                } else {
                                    if (users2.getAgentId().equals(users1.getAgentId())) {
                                        Users byParentAgentUserIdLP = userService.findByParentAgentUserIdLP(users2.getId(), users2.getAgentId());

                                        return getParentUserLevel(my, my.getAgentId(), byParentAgentUserIdLP, order, goodsByList, goodsByList1, order.getIsActivty());
                                        //  return parentWalletPlus((byParentAgentUserIdLP.getAgentId() == 2 && users1.getAgentId() == 2), my, order, byParentAgentUserIdLP, goodsByList, goodsByList1);
                                    } else {
                                        return getParentUserLevel(my, my.getAgentId(), users2, order, goodsByList, goodsByList1, order.getIsActivty());
                                    }
                                }
                            } else {
                                return PayMentReslt.fail(users2);
                            }
                        } else {
                            return PayMentReslt.success(users1);
                        }

                    } else {
                        //      PayMentReslt a = parentWalletPlus((users1.getAgentId() == 2 && byUserId.getAgentId() == 2), my, order, users1, goodsByList, goodsByList1);
                        //      users1 = a.getUsers();
                        return getParentUserLevel(my, my.getAgentId(), users1, order, goodsByList, goodsByList1, order.getIsActivty());
                        //       return a;
                    }

                } else {
                    return PayMentReslt.success(byUserId);

                }
            } else {
                return PayMentReslt.fail(byUserId);

            }


        } else {
            return PayMentReslt.fail(byUserId);

        }
   /*         userList.forEach((key, value) -> {
                System.out.println("KEY:" + key);
                System.out.println("NICK_NAME" + value.getNickname());

                switch (key) {
                    case 1:

                        if (level != null) {
                            if (!value.getAgentId().equals(byUserId.getAgentId())) {

                                level = one.getParentPrice();

                            }


                            BigDecimal bigDecimal = assignmentPriceSuan(goodsByList, goodsByList1, level);
                            boolean b = false;
                            try {
                                b = assignmentPrice(my, order.getId(), value, bigDecimal);
                                if (b) {
                                    index.set(1);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }

                            flag.set(b);
                            if (level2 == null) {
                                if (userList.containsKey(2)) {
                                    //    Users users = userList.get(1);
                                    if (!byUserId.getAgentId().equals(value.getAgentId())) {
                                        b = retPaymet1(my, value, order, goodsByList, goodsByList1, isActivty);
                                    }
                                }
                            }
                        } else {
                            flag.set(true);
                        }

                        break;
                    case 2:

                        if (level2 != null) {
                            Users users1 = userList.get(1);
                            if (!value.getAgentId().equals(users1.getAgentId())) {
                                level2 = one.getParentPrice();
                            }
                            BigDecimal bigDecimal2 = assignmentPriceSuan(goodsByList, goodsByList1, level2);
                            boolean b2 = false;
                            try {
                                b2 = assignmentPrice(my, order.getId(), value, bigDecimal2);
                                if (b2) {
                                    index.set(2);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            if (level3 == null) {
                                if (userList.containsKey(3)) {


                                    //  Users users = userList.get(2);
                                    if (!value.getAgentId().equals(users1.getAgentId())) {
                                        b2 = retPaymet1(my, value, order, goodsByList, goodsByList1, isActivty);
                                    }
                                }

                            }
                            flag.set(b2);
                        } else {
                            flag.set(true);
                        }
                        break;
                    case 3:

                        if (level3 != null) {
                            BigDecimal bigDecimal3 = assignmentPriceSuan(goodsByList, goodsByList1, level3);
                            boolean b3 = false;
                            try {
                                b3 = assignmentPrice(my, order.getId(), value, bigDecimal3);
                                if (b3) {
                                    index.set(3);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            flag.set(b3);
                        } else {
                            flag.set(true);
                        }
                        break;
                }

            });*/
         /*   if (index.get() < 3) {
                if (userList.containsKey(index.get())) {
                    Users users = userList.get(index.get());
                    if (users != null) {
                        Users byUserId1 = userService.findByUserId(users.getParentUserId());
                        boolean parentUserLevel1 = false;
                        try {
                            parentUserLevel1 = getParentUserLevel1(my, byUserId1.getAgentId(), byUserId1, orderId, goodsByList, goodsByList1);
                        } catch (AuthException e) {
                            e.printStackTrace();
                        }
                        flag.set(parentUserLevel1);
                    }
                }
            }*/

    }

    private PayMentReslt retTreeSuan(Users my, long myAgentId, BigDecimal level2, Users currentUser, Users currentUserp, OrderMaster order, AgentInvitationRelationship one, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws AuthException, SucsessException {
        Users parent = userService.findByUserId(currentUser.getParentUserId());

        if (!currentUser.getAgentId().equals(currentUserp.getAgentId())) {

            if (myAgentId == currentUserp.getAgentId()) {
                level2 = one.getLevel4();
            } else {
                level2 = one.getParentPrice();
            }
        }
        BigDecimal bigDecimal2 = assignmentPriceSuan(goodsByList, goodsByList1, level2);
        boolean b2 = assignmentPrice(my, order.getId(), currentUser, bigDecimal2);
        System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL0");
        if (b2) {
            if (!currentUser.getAgentId().equals(currentUserp.getAgentId())) {
                PayMentReslt b21 = retPaymet1(my, currentUser, order, goodsByList, goodsByList1, isActivty);
                System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL3");
                return b21;
            } else {
                System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL4");
                return PayMentReslt.success(currentUser);
            }
        } else {
            System.out.println("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL1");
            return PayMentReslt.fail(currentUser);
        }

    }

    private PayMentReslt retPaymet(Users my, Users byUserId1, OrderMaster order, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) {
        PayMentReslt parentUserLevel1 = PayMentReslt.fail(byUserId1);
        if (byUserId1 != null) {
            System.out.println("---------------USERS");
            System.out.println(byUserId1.getParentUserId());


            System.out.println("--------------------my-------------");
            System.out.println(my);
            System.out.println("------------------byUserId1----------------");
            System.out.println(byUserId1);
            try {
                parentUserLevel1 = getParentUserLevel1(my, byUserId1.getAgentId(), byUserId1, order, goodsByList, goodsByList1, isActivty);
            } catch (AuthException | SucsessException e) {
                e.printStackTrace();
            }


        }
        return parentUserLevel1;
    }

    private PayMentReslt retPaymet1(Users my, Users byUserId1, OrderMaster order, List<OrderDetail> goodsByList, List<OrderDetail> goodsByList1, Integer isActivty) throws SucsessException {
        PayMentReslt parentUserLevel1 = PayMentReslt.fail(byUserId1);
        if (byUserId1 != null) {
            System.out.println("---------------USERS");
            System.out.println(byUserId1.getParentUserId());


            System.out.println("--------------------my-------------");
            System.out.println(my);
            System.out.println("------------------byUserId1----------------");
            System.out.println(byUserId1);
            try {
                parentUserLevel1 = getParentUserLevel(my, byUserId1.getAgentId(), byUserId1, order, goodsByList, goodsByList1, isActivty);
            } catch (AuthException e) {
                e.printStackTrace();
            }


        }
        return parentUserLevel1;
    }

    @Transactional
    @Override
    public boolean saveShenheOrderDetails(OrderMaster orderMaster, List<OrderUserShenheParam> datas) {
        AtomicBoolean flag = new AtomicBoolean(false);
        Iterator<OrderUserShenheParam> iterator = datas.iterator();
        String msg = "مۇكاپات";
        Users byUserId = userService.findByUserId(orderMaster.getUserId());
        Users byUserIdParent = userService.findByUserId(byUserId.getParentUserId());

        AgentInvitationRelationshipLog log = new AgentInvitationRelationshipLog();
        log.setAgentId(orderMaster.getAgentId());
        log.setRelationAgentId(byUserIdParent.getAgentId());
        log.setOrderId(orderMaster.getId());
        log.setUserId(byUserId.getId());
        log.setRelationUserId(byUserId.getParentUserId());
        log.setPrice(orderMaster.getPaymentAmount());
        log.setType(2);
        boolean save1 = true;
        if (orderMaster.getIsUpgrade() == 1) {
            save1 = logService.save(log);
            flag.set(save1);
        }

        if (save1) {

            while (iterator.hasNext()) {
                OrderUserShenheParam next = iterator.next();
                boolean b = walletService.reChange(msg, orderMaster.getId(),orderMaster.getUserId(), next.getId(), next.getPrice(), 4);
                flag.set(b);
            }

            if (flag.get() && orderMaster.getIsUpgrade() == 1) {
                if (byUserId.getAgentId() != orderMaster.getAgentId()) {
                    byUserId.setAgentId(orderMaster.getAgentId());
                    byUserId.setAgentState(1);
                    userService.updateById(byUserId);
                }
            }
        }
        return flag.get();
    }

    @Transactional(rollbackFor = {RuntimeException.class, AuthException.class, Exception.class})
    @Override
    public boolean saveOrderCreate(OrderMaster order, List<OrderDetail> byOrderDetailList) throws AuthException, SucsessException {
        order.setQuantity(getCount(byOrderDetailList));
        System.out.println("agentType:" + agentType);
        System.out.println("------------------" + agentType + "------------------");
        System.out.println("------------------" + (agentType == 2) + "------------------");
        if (agentType == 2) {
            System.out.println("---------LLLLLLLLLLLLLLLLLLLLLLLLL---------" + agentType + "------------------");


            boolean b = saveOrderCreatePayment2(order, byOrderDetailList);
            System.out.println("---------LLLLLLLLLLLLLLLLLLLLLLLLL---------" + b + "------------------");
            if (b) {
                order.setIsPay(1);
                order.setPaymentStatus(2);
                order.setStatus(2);
                this.updateById(order);
                List<Long> parentUsersLevelIds = userService.getParentUsersLevelIds(order.getUserId());
                parentUsersLevelIds.add(order.getUserId());
                if (parentUsersLevelIds.size() > 0) {

                    return findByParenUsersPriceAll(parentUsersLevelIds);
                } else {
                    return true;
                }

            } else {
                return false;
            }
        } else if (agentType == 3) {
            AtomicBoolean flag = new AtomicBoolean(false);
            Users byUserId = userService.findByUserId(order.getUserId());
            //  Users byUserIdParent = userService.findByUserId(byUserId.getParentUserId());
            for (OrderDetail next : byOrderDetailList) {
                boolean b = saveOrderCreateImpl(order, byUserId, byUserId, next, 3);
                flag.set(b);
            }


            return flag.get();
        }
        if (agentType == 4) {
            Users byUserId = userService.findByUserId(order.getUserId());
            boolean b = saveAgentOrderDetailsPaymet4(order, byOrderDetailList, byUserId.getAgentId());
            if (b) {
                order.setIsPay(1);
                order.setPaymentStatus(2);
                order.setStatus(2);
                this.updateById(order);
                List<Long> parentUsersLevelIds = userService.getParentUsersLevelIds(order.getUserId());
                parentUsersLevelIds.add(order.getUserId());
                if (parentUsersLevelIds.size() > 0) {

                    return findByParenUsersPriceAll(parentUsersLevelIds);
                } else {
                    return true;
                }
            } else {
                return false;
            }

        } else {
            AtomicBoolean flag = new AtomicBoolean(false);
            Users byUserId = userService.findByUserId(order.getUserId());
            //  Users byUserIdParent = userService.findByUserId(byUserId.getParentUserId());
            for (OrderDetail next : byOrderDetailList) {
                boolean b = saveOrderCreateImpl(order, byUserId, byUserId, next, -1);
                flag.set(b);
            }

            return flag.get();

        }
    }

    @Override
    public boolean findAutoUpdate() {
        AtomicBoolean flag = new AtomicBoolean(false);
        List<Long> parentUsersLevelIds = userService.getFindChildrens(null);
        //  parentUsersLevelIds.add(order.getUserId());
        if (parentUsersLevelIds.size() > 0) {
            boolean byParenUsersAutoPriceAll = findByParenUsersPriceAll(parentUsersLevelIds);
            flag.set(byParenUsersAutoPriceAll);
        }

        return flag.get();

    }

    @Override
    public boolean findAutoByUpdate(long userId) {
        AtomicBoolean flag = new AtomicBoolean(false);
        List<Long> parentUsersLevelIds = userService.getFindChildrens(userId);
        if (parentUsersLevelIds == null) {
            parentUsersLevelIds = new ArrayList<>();
        }
        parentUsersLevelIds.add(userId);
        System.out.println("LLLLLLLLLLLLLLLLLLLLLL:" + parentUsersLevelIds.size());
        if (parentUsersLevelIds.size() > 0) {
            boolean byParenUsersAutoPriceAll = findByParenUsersPriceAll(parentUsersLevelIds);
            flag.set(byParenUsersAutoPriceAll);
        }

        return flag.get();
    }

    @Override
    public Map<String, Object> myWalletStastica(long userId) {
//SUM(PRICE) as value,MONTH(CREATE_DATE) as timePeriod
        Date date = new Date();
        List<Long> findChildrens = userService.getFindChildrens(userId);
        findChildrens.add(userId);

        Map<String, Object> map = new HashMap<>();
        MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        queryWrapper.dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_TIME, date, DateUtils.getDay(date, -7));
        List<BigDecimal> maps = this.listObjs(queryWrapper, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("weeks", maps);


        MyLambdaQueryWrapper<OrderMaster> queryWrapperWeekcount = new MyLambdaQueryWrapper<>();
        queryWrapperWeekcount.sum(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_TIME, date, DateUtils.getDay(date, -7)).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        BigDecimal obj1Weeek = this.getObj(queryWrapperWeekcount, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("weekscount", ObjectUtils.bigDecimalNotNull(obj1Weeek));

        MyLambdaQueryWrapper<OrderMaster> queryWrapperall = new MyLambdaQueryWrapper<>();
        queryWrapperall.sum(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        BigDecimal obj = this.getObj(queryWrapperall, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("yearcountallyear", ObjectUtils.bigDecimalNotNull(obj));

        MyLambdaQueryWrapper<OrderMaster> queryWrapperallout = new MyLambdaQueryWrapper<>();
        queryWrapperallout.sum(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        BigDecimal objout = this.getObj(queryWrapperallout, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("yearcountallyearout", ObjectUtils.bigDecimalNotNull(objout));


        MyLambdaQueryWrapper<OrderMaster> queryWrapperallyear = new MyLambdaQueryWrapper<>();
        queryWrapperallyear.sum(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_YEAR, date, DateUtils.getYear(date, -1)).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        BigDecimal obj1 = this.getObj(queryWrapperallyear, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("yearcountall", ObjectUtils.bigDecimalNotNull(obj1));


        MyLambdaQueryWrapper<OrderMaster> queryWrapperallyearout = new MyLambdaQueryWrapper<>();
        queryWrapperallyearout.sum(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_YEAR, date, DateUtils.getYear(date, -1)).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        BigDecimal obj1out = this.getObj(queryWrapperallyearout, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("yearcountallout", ObjectUtils.bigDecimalNotNull(obj1out));


        MyLambdaQueryWrapper<OrderMaster> queryWrapper1 = new MyLambdaQueryWrapper<>();
        queryWrapper1.select(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens);
        queryWrapper1.dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_MONTH, date, date).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        List<BigDecimal> maps1 = this.listObjs(queryWrapper1, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("month", maps1);


        MyLambdaQueryWrapper<OrderMaster> queryWrapperMontcount = new MyLambdaQueryWrapper<>();
        queryWrapperMontcount.sum(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens).dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_MONTH, date, date).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        BigDecimal obj1Month = this.getObj(queryWrapperMontcount, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("monthcount", ObjectUtils.bigDecimalNotNull(obj1Month));


        MyLambdaQueryWrapper<OrderMaster> queryWrapper3 = new MyLambdaQueryWrapper<>();
        queryWrapper3.select(OrderMaster::getPaymentAmount).in(OrderMaster::getUserId, findChildrens);
        queryWrapper3.dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_YEAR, date, DateUtils.getYear(date, -1)).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        List<BigDecimal> maps3 = this.listObjs(queryWrapper3, (o) -> BigDecimal.valueOf(Double.valueOf(o.toString())));
        map.put("years", maps3);


        //SELECT SUM(PRICE),TYPE FROM users_wallet_log GROUP BY TYPE
        MyLambdaQueryWrapper<OrderMaster> queryWrapper4 = new MyLambdaQueryWrapper<>();
        queryWrapper4.attr("userswithdrawaltype", OrderMaster::getPaymentMethod).as("name").sum(OrderMaster::getPaymentAmount).as("value").groupBy(OrderMaster::getPaymentMethod).in(OrderMaster::getUserId, findChildrens).eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        List<Map<String, Object>> maps4 = this.listMaps(queryWrapper4);
        map.put("oval", maps4);
        return map;
    }

    @Override
    public boolean agentDanTarget(OrderMaster orderMaster, List<OrderDetail> byOrderDetailList) {
        switch (agentType) {
            case 1:
                return xahtanService.danJianHuo(orderMaster, byOrderDetailList);

        }
        return false;
    }

  @Cacheable(value = "stastica", key = "'homeOrderChartStastic'")
    @Override
    public List<Map<String, Object>> homeOrderChartStastic() {
        Date date = new Date();
        MyLambdaQueryWrapper<OrderMaster> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.sum(OrderMaster::getPaymentAmount).as("price").select("DATE_FORMAT(CREATE_DATE,'月%m')").as("date")
                .last(true, "GROUP BY DATE_FORMAT(CREATE_DATE,'%Y-%m') ORDER BY create_date DESC ").eq(OrderMaster::getIsPay, 1).eq(OrderMaster::getStatus, 2);
        queryWrapper.dateBetween(OrderMaster::getCreateDate, DateFormat.DATE_TIME, DateUtils.getMonth(date, -12), date);
        return this.listMaps(queryWrapper);
    }

    @Override
    public Map<String, Object> info(long id) {
        Map<String, Object> map = new HashMap<>();
        OrderMaster order = this.getById(id);
        if(order.getIsShop()==1) {
            Map<String, Object> address = shopService.findMapById(order.getShopId());
            map.put("address", address);
        }else {
            UsersAddress address = usersAddressService.getById(order.getAddressId());
            map.put("address", address);
        }
        List<Map<String, Object>> details = detailService.findByOrderDetailList(order.getId());


        map.put("order", order);

        map.put("details", details);
        return map;
    }

    private boolean findByParenUsersAutoPriceAll(List<Long> parentUsersLevelIds) {
        AtomicBoolean flag = new AtomicBoolean(false);
        System.out.println("KKKKKKKKKKKKKKKKKKKKSIZE:" + parentUsersLevelIds.size());
        //  parentUsersLevelIds.add(order.getUserId());
        if (parentUsersLevelIds.size() > 0) {
            for (Long usersid : parentUsersLevelIds) {
                List<Long> findChildrens = userService.getFindChildrens(usersid);
                // findChildrens.add(usersid);
                if (findChildrens == null) {
                    findChildrens = new ArrayList<>();
                }
                System.out.println("-1--------------------USER_ID:" + usersid + "------------------");
                System.out.println(findChildrens.size());
                if (findChildrens.size() > 0) {
                    boolean b = kaspiNatijaDispose(usersid, findChildrens);
                    if (b) {

                        b = findByParenUsersAutoPriceAll(findChildrens);

                    }
                    System.out.println("findByParenUsersPriceAll:" + b);
                    flag.set(b);
                } else {
                    findChildrens.add(usersid);
                    boolean b = kaspiNatijaDispose(usersid, findChildrens);

                    System.out.println("findByParenUsersPriceAll:" + b);
                    flag.set(b);
                }

            }
        }

        return flag.get();
    }

    private boolean saveOrderCreatePayment2(OrderMaster order, List<OrderDetail> byOrderDetailList) throws AuthException, SucsessException {
        Long agentId = order.getAgentId();
        Users my = userService.findByUserId(order.getUserId());
        if (my.getParentUserId() != null) {
            Users byUserId = userService.findByUserId(my.getParentUserId());

            OrderDetail orderDetail = byOrderDetailList.get(0);
            return getCreateAssignmentSuan10(my, my.getAgentId(), order, orderDetail, byUserId, order.getIsActivty(), -1);
        } else {
            return true;
        }

    }

    private boolean saveOrderCreate(OrderMaster order, Users myUsers, Users parentUsers, OrderDetail detail, int type) throws AuthException {


        boolean a = false;

        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB4");
        if (order.getIsAgent() == 1) {
            myUsers.setAgentId(order.getAgentId());
            myUsers.setAgentState(1);
            userService.updateById(myUsers);
        }

        return true;


    }

    //@Transactional
    private boolean saveOrderCreateImpl(OrderMaster order, Users myUsers, Users parentUsers, OrderDetail detail, int type) throws AuthException {


        boolean a = false;
        if (type == 3) {
            a = getParentUserLevel3(myUsers, parentUsers, order, detail, type);
        } else {
            a = getParentUserLevel2(myUsers, parentUsers, order, detail, type);
        }
        if (a) {
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB4");
            if (order.getIsAgent() == 1) {
                myUsers.setAgentId(order.getAgentId());
                myUsers.setAgentState(1);
                userService.updateById(myUsers);
            }

            return true;
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
        }

    }

    private boolean getParentUserLevel2(Users my, Users users, OrderMaster order, OrderDetail detail, int type) throws AuthException {
        Users byUserId = userService.findByUserId(users.getParentUserId());
        if (byUserId != null) {
            System.out.println(users);
            System.out.println(byUserId);
            System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
            System.out.println(byUserId.getAgentId() < users.getAgentId());
            if (byUserId.getAgentId() < users.getAgentId()) {
                System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
                Users byUserId1 = userService.findByUserId(byUserId.getParentUserId());
                System.out.println("----------------------------");
                //   System.out.println(byUserId1);
                if (byUserId1.getAgentId().equals(users.getAgentId())) {
                    boolean createAssignmentSuan = getCreateAssignmentSuan1(my, users.getAgentId(), order, detail, byUserId1, order.getIsActivty(), type);
                    return createAssignmentSuan;
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId1.getAgentId(), users.getAgentId(), order.getIsActivty(), detail.getGoodsId(), type);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal bigDecimal = assignmentPriceSuan1(detail, one.getParentPrice());
                        boolean b = assignmentPrice(my, order.getId(), byUserId1, bigDecimal);

                        if (b) {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                            if (byUserId.getParentUserId() != null) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                boolean parentUserLevel = getParentUserLevel2(my, byUserId1, order, detail, type);
                                if (parentUserLevel) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                                }
                            }
                        }
                    }
                    return true;
                }


            } else {

                if (byUserId.getAgentId().equals(users.getAgentId())) {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5");

                    System.out.println("--------------byUserId------------------------");
                    System.out.println(byUserId);
                    System.out.println("--------------users------------------------");
                    System.out.println(users);
                    return getCreateAssignmentSuan1(my, users.getAgentId(), order, detail, byUserId, order.getIsActivty(), type);
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId.getAgentId(), users.getAgentId(), order.getIsActivty(), detail.getGoodsId(), type);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal parentPrice = one.getParentPrice();

                        if (parentPrice != null) {
                            BigDecimal bigDecimal = assignmentPriceSuan1(detail, one.getParentPrice());
                            boolean b = assignmentPrice(my, order.getId(), byUserId, bigDecimal);
                            if (b) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                                if (byUserId.getParentUserId() != null) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                    boolean parentUserLevel = getParentUserLevel2(my, byUserId, order, detail, type);
                                    if (parentUserLevel) {
                                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                                        return true;
                                    } else {
                                        return true;
                                    }
                                } else {
                                    return true;
                                }
                            } else {
                                return true;
                            }
                        } else {
                            return true;
                        }

                    } else {
                        return true;
                    }
                }

            }
        } else {
            return true;
        }

    }

    private boolean getParentUserLevel3(Users my, Users users, OrderMaster order, OrderDetail detail, int type) throws AuthException {
        Users byUserId = userService.findByUserId(users.getParentUserId());
        if (byUserId != null) {
            System.out.println(users);
            System.out.println(byUserId);
            System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
            System.out.println(byUserId.getAgentId() < users.getAgentId());
            if (byUserId.getAgentId() < users.getAgentId()) {
                System.out.println(byUserId.getAgentId() + "----|----" + users.getAgentId());
                Users byUserId1 = userService.findByUserId(byUserId.getParentUserId());
                System.out.println("----------------------------");
                //   System.out.println(byUserId1);
                if (byUserId1.getAgentId().equals(users.getAgentId())) {
                    boolean createAssignmentSuan = getCreateAssignmentSuan1(my, users.getAgentId(), order, detail, byUserId1, order.getIsActivty(), type);
                    return createAssignmentSuan;
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId1.getAgentId(), users.getAgentId(), order.getIsActivty(), detail.getGoodsId(), type);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal bigDecimal = assignmentPriceSuan1(detail, one.getParentPrice());
                        boolean b = assignmentPrice(my, order.getId(), byUserId1, bigDecimal);

                        if (b) {
                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");
                            if (byUserId.getParentUserId() != null) {
                                System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS7");
                                boolean parentUserLevel = getParentUserLevel3(my, byUserId1, order, detail, type);
                                if (parentUserLevel) {
                                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS8");
                                }
                            }
                        }
                    }
                    return true;
                }


            } else {
                if (byUserId.getAgentId().equals(users.getAgentId())) {
                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB5");

                    System.out.println("--------------byUserId------------------------");
                    System.out.println(byUserId);
                    System.out.println("--------------users------------------------");
                    System.out.println(users);
                    return getCreateAssignmentSuan1(my, users.getAgentId(), order, detail, byUserId, order.getIsActivty(), type);
                } else {
                    System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS4");
                    AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId.getAgentId(), users.getAgentId(), order.getIsActivty(), detail.getGoodsId(), type);
                    if (one != null) {
                        System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS5");
                        BigDecimal parentPrice = one.getParentPrice();

                        if (parentPrice != null) {
                            BigDecimal bigDecimal = assignmentPriceSuan1(detail, one.getParentPrice());


                            System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS6");


                            Map<Integer, Users> userList = userService.getParentUsersLevel(byUserId);
                            System.out.println("userList:" + userList.size());
                            //通过 lambda 表达式使用 forEach()
                            AtomicInteger index = new AtomicInteger(3);
                            AtomicBoolean flag = new AtomicBoolean(false);
                            userList.forEach((key, value) -> {
                                System.out.println("KEY:" + key);
                                System.out.println("NICK_NAME" + value.getNickname());
                                BigDecimal level = one.getLevel1();
                                BigDecimal level2 = one.getLevel2();
                                BigDecimal level3 = one.getLevel3();
                                switch (key) {
                                    case 1:


                                        if (level != null) {
                                            if (one.getIsLevel() == 1) {
                                                level = goPersent(level, order.getPaymentAmount());
                                            }
                                            BigDecimal bigDecimal1 = assignmentPriceSuan1(detail, level);
                                            boolean b = false;
                                            try {
                                                b = assignmentPrice(my, order.getId(), value, bigDecimal1);
                                                if (b) {
                                                    index.set(1);
                                                }
                                            } catch (AuthException e) {
                                                e.printStackTrace();
                                            }
                                            flag.set(b);
                                            if (level2 == null && level3 == null) {
                                                //   b = retPaymet1(my, value, order, detail,order.getIsActivty(),type);
                                            }
                                        } else {
                                            flag.set(true);
                                        }

                                        break;
                                    case 2:
                                        if (Objects.equals(byUserId.getAgentId(), value.getAgentId())) {

                                            if (level2 != null) {
                                                if (one.getIsLevel() == 1) {
                                                    level2 = goPersent(level2, order.getPaymentAmount());
                                                }
                                                BigDecimal bigDecimal2 = assignmentPriceSuan1(detail, level2);
                                                boolean b2 = false;
                                                try {
                                                    b2 = assignmentPrice(my, order.getId(), value, bigDecimal2);
                                                    if (b2) {
                                                        index.set(2);
                                                    }
                                                } catch (AuthException e) {
                                                    e.printStackTrace();
                                                }
                                                if (level3 == null) {
                                                    // b2 = retPaymet1(my, value, order, detail, order.getIsActivty(), type);
                                                }
                                                flag.set(b2);
                                            } else {
                                                flag.set(true);
                                            }
                                        } else {
                                            try {
                                                boolean b1 = assignmentPrice(my, order.getId(), value, bigDecimal);
                                                index.set(2);
                                                flag.set(b1);
                                            } catch (AuthException e) {
                                                throw new RuntimeException(e);
                                            }
                                        }
                                        break;

                                }

                            });

                            return flag.get();

                        } else {
                            return true;
                        }

                    } else {
                        return true;
                    }
                }

            }
        } else {
            return true;
        }

    }

    boolean getCreateAssignmentSuan1(Users my, long myAgentId, OrderMaster orderMaster, OrderDetail detail, Users byUserId, Integer isActivty, int type) throws AuthException {
        //   LoginLog loginLog = AuthConstant.getLoginLog();
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
        AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId.getAgentId(), myAgentId, isActivty, detail.getGoodsId(), type);
        if (one != null) {
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB7");
            System.out.println(one);
            Map<Integer, Users> userList = userService.getParentUsersLevel(byUserId);
            System.out.println("userList:" + userList.size());
            //通过 lambda 表达式使用 forEach()
            AtomicInteger index = new AtomicInteger(3);
            AtomicBoolean flag = new AtomicBoolean(false);
            userList.forEach((key, value) -> {
                System.out.println("KEY:" + key);
                System.out.println("NICK_NAME" + value.getNickname());
                BigDecimal level = one.getLevel1();
                BigDecimal level2 = one.getLevel2();
                BigDecimal level3 = one.getLevel3();
                switch (key) {
                    case 1:

                        if (level != null) {
                            if (one.getIsLevel() == 1) {
                                level = goPersent(level, orderMaster.getPaymentAmount());
                            }
                            BigDecimal bigDecimal = assignmentPriceSuan1(detail, level);
                            boolean b = false;
                            try {
                                b = assignmentPrice(my, orderMaster.getId(), value, bigDecimal);
                                if (b) {
                                    index.set(1);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            flag.set(b);
                            if (level2 == null && level3 == null) {
                                b = retPaymet1(my, value, orderMaster, detail, isActivty, type);
                            }
                        } else {
                            flag.set(true);
                        }

                        break;
                    case 2:

                        if (level2 != null) {
                            if (one.getIsLevel() == 1) {
                                level2 = goPersent(level2, orderMaster.getPaymentAmount());
                            }
                            BigDecimal bigDecimal2 = assignmentPriceSuan1(detail, level2);
                            boolean b2 = false;
                            try {
                                b2 = assignmentPrice(my, orderMaster.getId(), value, bigDecimal2);
                                if (b2) {
                                    index.set(2);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            if (level3 == null) {
                                b2 = retPaymet1(my, value, orderMaster, detail, isActivty, type);
                            }
                            flag.set(b2);
                        } else {
                            flag.set(true);
                        }
                        break;
                    case 3:

                        if (level3 != null) {
                            if (one.getIsLevel() == 1) {
                                level3 = goPersent(level3, orderMaster.getPaymentAmount());
                            }
                            BigDecimal bigDecimal3 = assignmentPriceSuan1(detail, level3);
                            boolean b3 = false;
                            try {
                                b3 = assignmentPrice(my, orderMaster.getId(), value, bigDecimal3);
                                if (b3) {
                                    index.set(3);
                                }
                            } catch (AuthException e) {
                                e.printStackTrace();
                            }
                            flag.set(b3);
                        } else {
                            flag.set(true);
                        }
                        break;
                }

            });
         /*   if (index.get() < 3) {
                if (userList.containsKey(index.get())) {
                    Users users = userList.get(index.get());
                    if (users != null) {
                        Users byUserId1 = userService.findByUserId(users.getParentUserId());
                        boolean parentUserLevel1 = false;
                        try {
                            parentUserLevel1 = getParentUserLevel1(my, byUserId1.getAgentId(), byUserId1, orderId, goodsByList, goodsByList1);
                        } catch (AuthException e) {
                            e.printStackTrace();
                        }
                        flag.set(parentUserLevel1);
                    }
                }
            }*/
            return flag.get();
        } else {
            throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
        }

    }

    boolean getCreateAssignmentSuan10(Users my, long myAgentId, OrderMaster order, OrderDetail detail, Users byUserId, Integer isActivty, int ptype) throws AuthException, SucsessException {
        List<OrderDetail> goodsByList, goodsByList1;
        goodsByList = new ArrayList<>();
        goodsByList.add(detail);
        goodsByList1 = new ArrayList<>();
        //   LoginLog loginLog = AuthConstant.getLoginLog();
        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB6");
        // Users byUserId = null;
/*
        Users byUserIdParent = null;
        Users byUserIdParentParent = null;
        if (my.getParentUserId() != null) {
            //  byUserId = userService.findByUserId(my.getParentUserId());
            if (byUserId.getParentUserId() != null) {
                byUserIdParent = userService.findByUserId(byUserId.getParentUserId());

                if (byUserIdParent.getParentUserId() != null) {
                    byUserIdParentParent = userService.findByUserId(byUserIdParent.getParentUserId());
                }
                System.out.println(byUserId);
            }
        }
*/


        System.out.println(byUserId);
        System.out.println(order);
        if (byUserId != null) {
            PayMentReslt a1 = getParentUserLevel(my, my.getAgentId(), my, order, goodsByList, goodsByList1, order.getIsActivty());
            return a1.isSuccess();
    /*        AgentInvitationRelationship one = null;

            // AgentInvitationRelationship one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId.getAgentId(), myAgentId, isActivty, detail.getGoodsId(), type);

            one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId.getAgentId(), my.getAgentId(), isActivty, detail.getGoodsId(), ptype);


            //  byUserId.setAgentId(agentId);
            if (one != null) {
               *//* if (one.getIsPrice() == 1) {
                    one.setPrice(goPersent(order.getPaymentAmount(), one.getPrice()));
                }*//*
                int type = 1;
                BigDecimal price = one.getParentPrice();

                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-7");
                AtomicBoolean flag = new AtomicBoolean(false);
                if (price != null) {

                    boolean b = assignmentPrice(my, order.getId(), byUserId, price);
                    flag.set(b);
                }

                if (byUserIdParent != null) {
                    PayMentReslt a1 = getParentUserLevel(my, byUserIdParent.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());
                    flag.set(a1.isSuccess());
              *//*      if (one.getLevel1() != null) {
                        System.out.println("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP1-----------");
                        System.out.println(byUserId.getAgentId() + "!=" + byUserIdParent.getAgentId());
                        System.out.println(byUserId.getPhone() + "--" + byUserId.getAgentId() + "!=" + byUserIdParent.getAgentId() + "--" + byUserIdParent.getAgentId());

                        System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-9-" + byUserIdParent.getId());
                        if (byUserId.getAgentId() != byUserIdParent.getAgentId()) {
                            one.setLevel1(one.getLevel1());
                        } else {
                            if (one.getIsLevel() == 1) {
                                one.setLevel1(goPersent(order.getPaymentAmount(), one.getLevel1()));
                            }
                        }

                        BigDecimal bigDecimal = assignmentPriceSuan1(detail, one.getLevel1());
                        boolean b = assignmentPrice(my, order.getId(), byUserIdParent, bigDecimal);
                        flag.set(b);

                        if (byUserIdParentParent != null) {
                            if (one.getLevel2() != null) {
                                System.out.println("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP2-----------");
                                System.out.println(byUserIdParent.getPhone() + "--" + byUserIdParent.getAgentId() + "!=" + byUserIdParentParent.getPhone() + "--" + byUserIdParentParent.getAgentId());
                                if (byUserId.getAgentId() != byUserIdParent.getAgentId()) {
                                    one.setLevel2(one.getParentPrice());
                                } else {
                                    if (one.getIsLevel() == 1) {
                                        one.setLevel2(goPersent(order.getPaymentAmount(), one.getLevel2()));
                                    }
                                }
                                System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-10-" + byUserIdParentParent.getId());
                                BigDecimal bigDecimal1 = assignmentPriceSuan1(detail, one.getLevel2());
                                boolean b1 = assignmentPrice(my, order.getId(), byUserIdParentParent, bigDecimal1);

                                if (b1) {
                                    PayMentReslt a1 = getParentUserLevel(my, byUserIdParent.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());
                                    byUserId = null;
                                    byUserIdParent = null;
                                    //  byUserIdParentParent = null;
                                    if (byUserIdParentParent.getParentUserId() != null) {
                                        byUserId = userService.findByUserId(byUserIdParentParent.getParentUserId());
                                        if (byUserIdParentParent.getAgentId() == byUserId.getAgentId()) {
                                            byUserIdParent = userService.findByUserId(byUserId.getParentUserId());
                                            //    byUserIdParentParent = userService.findByUserId(byUserIdParent.getParentUserId());

                                            one = agentInvitationRelationshipService.getAgentInfoImpl(byUserId.getAgentId(), byUserIdParentParent.getAgentId(), isActivty, detail.getGoodsId(), ptype);


                                            if (byUserId != null) {
                                                if (one.getLevel1() != null) {
                                                    System.out.println("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP1-----------");
                                                    System.out.println(byUserId.getAgentId() + "!=" + byUserIdParentParent.getAgentId());
                                                    System.out.println(byUserId.getPhone() + "--" + byUserId.getAgentId() + "!=" + byUserIdParentParent.getAgentId() + "--" + byUserIdParentParent.getAgentId());

                                                    System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-9-" + byUserIdParentParent.getId());
                                                    if (byUserId.getAgentId() != byUserIdParentParent.getAgentId()) {
                                                        one.setLevel1(one.getParentPrice());
                                                    } else {
                                                        if (one.getIsLevel() == 1) {
                                                            one.setLevel1(goPersent(order.getPaymentAmount(), one.getLevel1()));
                                                        }
                                                    }

                                                    bigDecimal = assignmentPriceSuan1(detail, one.getLevel1());
                                                    b = assignmentPrice(my, order.getId(), byUserId, bigDecimal);
                                                    flag.set(b);
                                                    if (byUserIdParent != null) {
                                                        if (one.getLevel2() != null) {
                                                            System.out.println("PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP2-----------");
                                                            System.out.println(byUserId.getPhone() + "--" + byUserId.getAgentId() + "!=" + byUserIdParent.getPhone() + "--" + byUserIdParent.getAgentId());
                                                            if (byUserId.getAgentId() != byUserIdParent.getAgentId()) {
                                                                one.setLevel2(one.getParentPrice());
                                                            } else {
                                                                if (one.getIsLevel() == 1) {
                                                                    one.setLevel2(goPersent(order.getPaymentAmount(), one.getLevel2()));
                                                                }
                                                            }
                                                            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB-10-" + byUserIdParent.getId());
                                                            bigDecimal = assignmentPriceSuan1(detail, one.getLevel2());
                                                            b = assignmentPrice(my, order.getId(), byUserIdParent, bigDecimal);

                                                            if (b) {
                                                                PayMentReslt a1 = getParentUserLevel(my, byUserIdParent.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());
                                                                flag.set(a1.isSuccess());
                                                                b=a1.isSuccess();

                                                        //        PayMentReslt a = parentWalletPlus((byUserId.getAgentId() == 2 && byUserIdParent.getAgentId() == 2), my, order, byUserIdParent, goodsByList, goodsByList1);

                                                            }
                                                            flag.set(b);
                                                        } else {
                                                           // PayMentReslt a = parentWalletPlus((byUserIdParentParent.getAgentId() == 2 && byUserId.getAgentId() == 2), my, order, byUserId, goodsByList, goodsByList1);
                                                            PayMentReslt a1 = getParentUserLevel(my, byUserIdParent.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());
                                                            flag.set(a1.isSuccess());
                                                        }
                                                    } else {
                                                      //  PayMentReslt a = parentWalletPlus((byUserIdParentParent.getAgentId() == 2 && byUserId.getAgentId() == 2), my, order, byUserId, goodsByList, goodsByList1);
                                                        PayMentReslt a1 = getParentUserLevel(my, byUserIdParent.getAgentId(), byUserId, order, goodsByList, goodsByList1, order.getIsActivty());
                                                        flag.set(a1.isSuccess());
                                                    }
                                                }

                                            }
                                        }
                                    }
                                }

                                flag.set(b);
                            } else {
                                PayMentReslt a = parentWalletPlus((byUserId.getAgentId() == 2 && byUserIdParent.getAgentId() == 2), my, order, byUserIdParent, goodsByList, goodsByList1);
                            }
                        }
                    }*//*
                }

                return flag.get();


            } else {
                throw new AuthException(ResultCode.REQUEST_ERROR_PAYMENT_REQUIRED);
            }*/
        } else {
            return false;
        }

    }

    private boolean retPaymet1(Users my, Users byUserId1, OrderMaster orderMaster, OrderDetail detail, Integer isActivty, int type) {
        boolean parentUserLevel1 = false;
        if (byUserId1 != null) {
            System.out.println("---------------USERS");
            System.out.println(byUserId1.getParentUserId());


            System.out.println("--------------------my-------------");
            System.out.println(my);
            System.out.println("------------------byUserId1----------------");
            System.out.println(byUserId1);
            try {
                parentUserLevel1 = getParentUserLevel2(my, byUserId1, orderMaster, detail, type);
            } catch (AuthException e) {
                e.printStackTrace();
            }


        }
        return parentUserLevel1;
    }

    private BigDecimal assignmentPriceSuan1(OrderDetail next, BigDecimal price) {
        BigDecimal suanTotal = new BigDecimal(0);


        BigDecimal parentPrice = new BigDecimal(0);
        parentPrice = parentPrice.add(price);
        parentPrice = parentPrice.multiply(BigDecimal.valueOf(next.getQuantity()));
        suanTotal = suanTotal.add(parentPrice);


        return suanTotal;
    }

    private BigDecimal goPersent(BigDecimal number, BigDecimal p) {
        BigDecimal divide = p.divide(new BigDecimal(100));
        return number.multiply(divide);
    }

    private BigDecimal goPriceValid(Long agentId, Long agentId1, BigDecimal p) {
        if (agentId1.equals(agentId)) {
            if (agentId.equals(2)) {
                BigDecimal pp = new BigDecimal(60);
                return pp;
            }
            if (agentId.equals(3)) {
                BigDecimal pp = new BigDecimal(30);
                return pp;
            }
            return p;
        } else {
            return p;
        }

    }
}




